﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ManageBanking.Models;


namespace ManageBanking.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        LoginModels lg = new LoginModels();
        T_AccountModels ac = new T_AccountModels();
        public ActionResult Index()
        {
            return View();
        }
       
        public ActionResult Home()
        {
            ViewData["menutopvalue"] = "Home";
            ViewData["listnews"] = lg.ListNews();
            return View();
        }
        public ActionResult ResolveLogin(string username, string password)
        {
            
            if (lg.Login_Bank2(username) == false)
            {
                Session["CheckUser"] = false;
                return RedirectToAction("Home");
            }
            else if(lg.Login_Bank2(username) == true){
                Username ac1 = lg.Login_Acc(username);
                if (ac1.Lock == true)
                {
                    Session["CheckUser1"] = false;
                    return RedirectToAction("Home");
                }
                else if (lg.Login_Bank(username, password) == true)
                {

                    Username ac = lg.Login_Acc(username);
                    if (ac.LastLogin == null)
                    {
                        Session.Remove("CheckUser");
                        Session.Remove("count");
                        Session.Remove("Check3");
                        Session.Remove("CheckUser1");
                        DateTime dt1 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); ;
                        Session["Login"] = ac;
                        lg.updatelasttime(username);
                        ac.LastLogin = dt1;
                        Session["Login"] = ac;
                        return RedirectToAction("ChangeInformation", "ManageUser");
                    }
                    else if (ac.IdTypeUser == 2)
                    {
                        Session.Remove("CheckUser");
                        Session.Remove("count");
                        Session.Remove("Check3");
                        Session.Remove("CheckUser1");
                        DateTime dt1 = ac.LastLogin.Value;

                        Session["Login"] = ac;
                        lg.updatelasttime(username);
                        ac.LastLogin = dt1;
                        Session["Login"] = ac;
                        return RedirectToAction("ChangeInformation","ManageUser");
                    }
                    else
                    {
                        Session.Remove("CheckUser");
                        Session.Remove("count");
                        Session.Remove("Check3");
                        Session.Remove("CheckUser1");
                        DateTime dt1 = ac.LastLogin.Value;

                        Session["Login"] = ac;
                        lg.updatelasttime(username);
                        ac.LastLogin = dt1;
                        Session["Login"] = ac;
                        return RedirectToAction("ChangeInformation", "ManageUser");
                    }
                }
                 else 
            {
                Username accheck = lg.Login_Acc(username);
                if (accheck.IdTypeUser == 2)
                {
                    Session.Remove("CheckUser");
                    Session.Remove("CheckUser1");
                    Session.Remove("Check3");
                    if (Session["count"] == null)
                        Session["count"] = 0;
                    int a = (int)Session["count"];
                    a++;
                    Session["count"] = a;
                    if (a == 3)
                        lg.AddLock(username);
                    return RedirectToAction("Home");
                }
                else
                {
                    Session["Check3"] = false;
                    return RedirectToAction("Home");
                
                }
            }
            
            }
            else
            {
                Username accheck = lg.Login_Acc(username);
                if (accheck.IdTypeUser == 2)
                {
                    Session.Remove("CheckUser");
                    Session.Remove("CheckUser1");
                    Session.Remove("Check3");
                    if (Session["count"] == null)
                        Session["count"] = 0;
                    int a = (int)Session["count"];
                    a++;
                    Session["count"] = a;
                    if (a == 3)
                        lg.AddLock(username);
                    return RedirectToAction("Home");
                }
                else
                {
                    Session["Check3"] = false;
                    return RedirectToAction("Home");

                }
            }
             
           
        }
        public ActionResult Logout()
        {
            Session["Login"] = null;
            return RedirectToAction("Home");

        }

        public ActionResult Google()
        {

            return View("Google");

        }
        /*
        public ActionResult Search(string search)
        {
            string url = "http://www.google.com/#hl=vi&output=search&sclient=psy-ab&q="+search+"&oq="+search+"&gs_l=hp.3..0l4.1203.2240.0.2994.8.8.0.0.0.0.924.6251.4-1j1j6.8.0...0.0.VPenrARWQfg&pbx=1&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&fp=73fd4771307927dd&biw=1024&bih=645";
            GoogleSearchService g = new GoogleSearchService();
            GoogleSearchResult gsr = g.doGoogleSearch("may tinh", "may tinh", 0, 10, true, "", true, "", "", "");
            int n = gsr.resultElements.Count();

            return  Redirect(url);
        }*/

    }
}
